#include <iostream>
#include <algorithm>
using namespace std;

int n,post[35],in[35],cnt = 0;
struct Node{
    int data;
    struct Node *left,*right;
};

Node *create(int l1,int r1,int l2,int r2){
    if(l1>r1||l2>r2)    return NULL;
    Node *root = new Node;
    root->data = post[r1];
    root->left = root->right = NULL;
    int index = l2;
    while(in[index]!=post[r1])    index++;
    root->left = create(l1,l1+index-1-l2,l2,index-1);
    root->right = create(l1+index-l2,r1-1,index+1,r2);
    return root;
}

signed main()
{
    cin>>n;
    for(int i=0;i<n;i++)    cin>>in[i];
    for(int i=0;i<n;i++)    cin>>post[i];
    Node *root = create(0,n-1,0,n-1);
    vector<Node*> v,t;
    v.push_back(root);
    while(v.size()){
        t.clear();
        for(int i=0;i<v.size();i++){
            if(v[i]->left)    t.push_back(v[i]->left);
            if(v[i]->right)    t.push_back(v[i]->right);
        }
        if(cnt%2==0)    reverse(v.begin(),v.end());
        for(int i=0;i<v.size();i++){
            if(cnt)    cout<<" ";
            cout<<v[i]->data;
        }
        v = t;
        cnt++;
    }
    return 0;
}
